/*
 * @(#)MPQueryRequestTransformer.java
 * Date 2013-10-14
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.mpq.transformer.model;

import javax.annotation.PostConstruct;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.SeverityType;
import us.hornerscorners.lamppost.exception.ValidationException;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEServiceType;
import us.hornerscorners.lamppost.ihecommon.config.RegistryServiceConfig;
import us.hornerscorners.lamppost.ihecommon.config.TestHack;

import us.hornerscorners.lamppost.mpq.inject.MPQTarget;

import us.hornerscorners.lamppost.registry.config.AdhocQueryConfig;
import us.hornerscorners.lamppost.registry.config.QueryMethodConfig;

import us.hornerscorners.lamppost.registry.constant.ErrorCode;
import us.hornerscorners.lamppost.registry.constant.QueryName;
import us.hornerscorners.lamppost.registry.constant.QuerySlotName;

import us.hornerscorners.lamppost.registry.model.QueryRequest;

import us.hornerscorners.lamppost.registry.transformer.model
    .AbstractQueryRequestTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-14
 * @author         Jim Horner
 */
@ApplicationScoped
public class MPQueryRequestTransformer extends AbstractQueryRequestTransformer {

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** Field description */
    @Inject
    @MPQTarget("META-INF/MultiPatientQueryConfig.yml")
    private AdhocQueryConfig queryConfig;

    /** Field description */
    private RegistryServiceConfig serviceConfig;

    /**
     * Constructs ...
     *
     */
    public MPQueryRequestTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    protected AdhocQueryConfig getQueryConfig() {

        return this.queryConfig;
    }

    /**
     * Method description
     *
     */
    @PostConstruct
    public void postConstruct() {

        this.serviceConfig = (RegistryServiceConfig) this.config.findServiceConfig(
            IHEServiceType.Registry);
    }

    /**
     * Method description
     *
     *
     * @param methodConfig
     * @param result
     *
     * @throws ValidationException
     */
    @Override
    protected void validateRequired(QueryMethodConfig methodConfig, QueryRequest result)
            throws ValidationException {

        if (QueryName.FindDocumentsForMultiplePatients.equals(
                methodConfig.getQueryName())) {

            // at least one of ClassCode, EventCodeList or
            // HealthcareFacilityTypeCode shall be specified
            if (result.hasNoValues(QuerySlotName.XDSDocumentEntryClassCode)
                    && result.hasNoValues(QuerySlotName.XDSDocumentEntryEventCodeList)
                    && result.hasNoValues(
                        QuerySlotName.XDSDocumentEntryHealthcareFacilityTypeCode)) {

                String msg =
                    String.format(
                        "Slot [%s] or Slot [%s] or Slot [%s] (one of them) is required and none contains a valid value.",
                        QuerySlotName.XDSDocumentEntryClassCode.getCode(),
                        QuerySlotName.XDSDocumentEntryEventCodeList.getCode(),
                        QuerySlotName.XDSDocumentEntryHealthcareFacilityTypeCode.getCode());

                ServiceActionError error =
                    new ServiceActionError(
                        ErrorCode.XDSStoredQueryMissingParam.getCode(), msg,
                        SeverityType.Error);

                if (this.serviceConfig.useTestHack(TestHack.XDS12364)) {

                    logger.warn("Using TestHack {}, *NOT* throwing ValidationException.",
                                TestHack.XDS12364);

                } else {

                    // NOTE Test 12364 (2013-10-11) does not like this!!
                    throw new ValidationException(getErrorLocation(), error);
                }
            }
        }
    }
}
